package Adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qb.devserv.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import utils.MyUtil;

public class TypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mCtx;
    private JSONArray mData;
    private List<Boolean> isClicks;
    private OnItemClickListener mOnItemClickListener = null;

    public TypeAdapter(Context ctx, JSONArray data) {
        mCtx = ctx;
        mData = data;
        isClicks = new ArrayList<>();
        for (int i = 0; i < data.length(); i++) {
            isClicks.add(false);
            if (i == 0) {
                isClicks.set(0, true);
            }
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_type_layout, parent, false);
        return new MHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MHolder mHolder = (MHolder) holder;
        try {
            JSONObject o = new JSONObject(mData.get(position).toString());
            Drawable drawable = mCtx.getResources().getDrawable(o.getInt("icon"));
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),drawable.getMinimumHeight());
            mHolder.typeIcon.setCompoundDrawables(drawable, null, null, null);
            mHolder.typeName.setText(o.getString("label"));

            if (isClicks.get(position)) {
                mHolder.llType.setBackgroundColor(Color.parseColor("#F5F5F5"));
            } else {
                mHolder.llType.setBackgroundColor(Color.parseColor("#EEEEEE"));
            }
            if (mOnItemClickListener != null) {
                mHolder.llType.setOnClickListener(view -> {
                    selectedEffect(position);
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return mData.length();
    }

    public class MHolder extends RecyclerView.ViewHolder {
        public LinearLayout llType;
        public TextView typeName,typeIcon;
        public MHolder(View v) {
            super(v);
            llType = v.findViewById(R.id.ll_type);
            typeName = v.findViewById(R.id.tv_type_name);
            typeIcon = v.findViewById(R.id.tv_type_icon);
        }
    }

    //点击事件
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void selectedEffect(int position) {
        for (int i = 0; i < isClicks.size(); i++) {
            isClicks.set(i, false);
        }
        isClicks.set(position, true);
        notifyDataSetChanged();
    }
}
